package com.gua.jbs.serviceImpl;

import java.io.*;
import java.util.*;

import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.gua.jbs.controller.LoginController;
import com.gua.jbs.dao.IUploadDao;
import com.gua.jbs.daoImpl.UploadDao;
import com.gua.jbs.service.IUploadService;
import com.gua.jbs.utility.ApplicationConstant;
import com.gua.jbs.utility.ApplicationException;

public class UploadService extends BaseService implements IUploadService{
	
	private static final Logger logger = Logger
			.getLogger(LoginController.class);

	boolean isUploadExcelFile=false;
	IUploadDao uploadDao=new UploadDao();
	public boolean uploadProdType(String fileName) throws ApplicationException {
		
		XSSFWorkbook workbook=null;
        XSSFSheet sheet=null;
        //HSSFWorkbook hwb=null;
        //HSSFSheet hssfSheet=null;
        // fileinputStream =null;
        Map<String, List<String>> colour_map=new HashMap<String, List<String>>();
        File file= null;
        String msg="";
        Iterator<Row> rowIterator=null;
       try
       {
           file= new File(ApplicationConstant.EXCEL_FILE_PATH+fileName);
           FileInputStream fileinputStream = new FileInputStream(file);
           logger.info("file==="+fileinputStream.getFD());
           logger.info("file1==="+file.exists());
           //Create Workbook instance holding reference to .xlsx file
           workbook=new XSSFWorkbook(fileinputStream);
           sheet = workbook.getSheetAt(0);
           //Get first/desired sheet from the workbook
           /*if(msg.equalsIgnoreCase("XSSFWorkbookFails")){
               file= new File("/home/yogesh/Downloads/Liferay/howtodoinjava_demo.xlsx");
               fileinputStream = new FileInputStream(file);
               hwb=new HSSFWorkbook(fileinputStream);
               hssfSheet =  hwb.getSheetAt(0);
           }
*/
           //Iterate through each rows one by one
           if(sheet!=null){
               rowIterator = sheet.iterator();
           }/*else{
               rowIterator = hssfSheet.iterator();
           }*/
           
           int count=1;
           String color="color";
           while (rowIterator.hasNext())
           {
               Row row = rowIterator.next();
               //For each row, iterate through all the columns
               Iterator<Cell> cellIterator = row.cellIterator();
               List<String> colorList=new ArrayList<String>();
               while (cellIterator.hasNext())
               {
                   Cell cell = cellIterator.next();
                   //Check the cell type and format accordingly
                   switch (cell.getCellType())
                   {
                       case Cell.CELL_TYPE_NUMERIC:
                    	   //logger.info(cell.getNumericCellValue() + "  ");
                           colorList.add(""+cell.getNumericCellValue());
                           break;
                       case Cell.CELL_TYPE_STRING:
                    	   //logger.info(cell.getStringCellValue() + "  ");
                           colorList.add(cell.getStringCellValue());
                           break;
                   }
               }
               if(!colorList.isEmpty()){
                   colour_map.put(color+count, colorList);
                   count++;
              }
              
           }
           fileinputStream.close();
           
           logger.info("colour_map-=="+colour_map.size()+"=="+colour_map);
       }
       catch (Exception e){
           e.printStackTrace();
       }
 		
		//isUploadExcelFile=uploadDao.saveProdType();
		
		return isUploadExcelFile;
	}

	public boolean uploadProdSubType(String fileName)
			throws ApplicationException {
		return isUploadExcelFile;
	}

	public boolean uploadProdColor(String fileName) throws ApplicationException {
		return isUploadExcelFile;
	}

	@Override
	public boolean uploadProdPolish(String fileName)
			throws ApplicationException {
		// TODO Auto-generated method stub
		return isUploadExcelFile;
	}

}
